package com.mega.fe.render;


import com.mega.fe.mixins.AccessGuiGraphics;
import com.mega.fe.render.shaders.ShaderInit;
import com.mojang.blaze3d.platform.TextureUtil;
import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.shaders.Uniform;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipPositioner;
import net.minecraft.client.gui.screens.inventory.tooltip.DefaultTooltipPositioner;
import net.minecraft.client.gui.screens.inventory.tooltip.TooltipRenderUtil;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.PostChain;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.event.RenderGuiOverlayEvent;
import net.minecraftforge.client.event.RenderTooltipEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.joml.Matrix4f;
import org.joml.Vector2ic;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;

@Mod.EventBusSubscriber
public class RendererUtils {

    public static float time = 0F;
    public static PostChain postEffect;
    public static Minecraft minecraft = Minecraft.getInstance();

    @SubscribeEvent(priority = EventPriority.HIGH)
    public static void gui(RenderGuiOverlayEvent event) {
        GuiGraphics guiGraphics = new GuiGraphics(Minecraft.getInstance(), Minecraft.getInstance().renderBuffers().bufferSource());
        PoseStack stack = new PoseStack();
        stack.pushPose();
        stack.scale(0.01f, 0.01f, 0.01f);
        guiGraphics.renderItem(new ItemStack(Items.ENCHANTED_GOLDEN_APPLE), -13, -12);
        stack.popPose();
    }

    public static void renderBackground() {
        //System.out.println(time);
        GuiGraphics guiGraphics = new GuiGraphics(minecraft, minecraft.renderBuffers.bufferSource()) ;
        Window window = minecraft.getWindow();
        int x = window.getGuiScaledWidth();
        int y = window.getGuiScaledHeight();
        Matrix4f matrix4f = guiGraphics.pose().last().pose();
        ShaderInstance instance = ShaderInit.getRAIN();
        if (instance != null) {
            ShaderGetter.updateUniform_core(instance, "time", time);
            ShaderGetter.updateUniform_core(instance, "iResolution", new float[] {800, 450});
            RenderSystem.setShaderTexture(0, new ResourceLocation("fe", "textures/RealGod.png"));
            //instance.setSampler("Sampler0", TextureUtil.generateTextureId());
        }
        RenderSystem.setShader(ShaderInit::getRAIN);
        BufferBuilder bufferBuilder = Tesselator.getInstance().getBuilder();
        bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
        bufferBuilder.vertex(matrix4f, 0, 0, 0).uv(0, 0).endVertex();
        bufferBuilder.vertex(matrix4f, 0, y, 0).uv(0, 1).endVertex();
        bufferBuilder.vertex(matrix4f, x, y, 0).uv(1, 1).endVertex();
        bufferBuilder.vertex(matrix4f, x, 0, 0).uv(1, 0).endVertex();
        BufferUploader.drawWithShader(bufferBuilder.end());
    }

    public static void renderTooltip(GuiGraphics guiGraphics, ItemStack tooltipStack, Font p_282308_, int p_282687_, int p_282292_) {
        renderTooltip(guiGraphics, tooltipStack, p_282308_, Screen.getTooltipFromItem(Minecraft.getInstance(), tooltipStack), tooltipStack.getTooltipImage(), p_282687_, p_282292_);
    }

    public static void renderTooltip(GuiGraphics guiGraphics, ItemStack tooltipStack, Font p_283128_, List<Component> p_282716_, Optional<TooltipComponent> p_281682_, int p_283678_, int p_281696_) {
        List<ClientTooltipComponent> list = ForgeHooksClient.gatherTooltipComponents(tooltipStack, p_282716_, p_281682_, p_283678_, guiGraphics.guiWidth(), guiGraphics.guiHeight(), p_283128_);
        renderTooltipInternal(guiGraphics, tooltipStack, p_283128_, list, p_283678_, p_281696_, DefaultTooltipPositioner.INSTANCE);
    }

    public static void renderTooltipInternal(GuiGraphics guiGraphics, ItemStack tooltipStack, Font p_282675_, List<ClientTooltipComponent> p_282615_, int p_283230_, int p_283417_, ClientTooltipPositioner p_282442_) {
        if (!p_282615_.isEmpty()) {
            RenderTooltipEvent.Pre preEvent = new RenderTooltipEvent.Pre(tooltipStack, guiGraphics, p_283230_, p_283417_, guiGraphics.guiWidth(), guiGraphics.guiHeight(), p_282675_, p_282615_, p_282442_);


            int i = 0;
            int j = p_282615_.size() == 1 ? -2 : 0;

            ClientTooltipComponent clienttooltipcomponent;
            for (Iterator<ClientTooltipComponent> var9 = p_282615_.iterator(); var9.hasNext(); j += clienttooltipcomponent.getHeight()) {
                clienttooltipcomponent = var9.next();
                int k = clienttooltipcomponent.getWidth(preEvent.getFont());
                if (k > i) {
                    i = k;
                }
            }

            Vector2ic vector2ic = p_282442_.positionTooltip(guiGraphics.guiWidth(), guiGraphics.guiHeight(), preEvent.getX(), preEvent.getY(), i, j);
            int l = vector2ic.x();
            int i1 = vector2ic.y();
            ((AccessGuiGraphics) (guiGraphics)).pose().pushPose();
            //int j1 = true;
            int finalI = i;
            int finalJ = j;
            guiGraphics.drawManaged(() -> {
                RenderTooltipEvent.Color colorEvent = ForgeHooksClient.onRenderTooltipColor(tooltipStack, guiGraphics, l, i1, preEvent.getFont(), p_282615_);
                TooltipRenderUtil.renderTooltipBackground(guiGraphics, l, i1, finalI, finalJ, 400, colorEvent.getBackgroundStart(), colorEvent.getBackgroundEnd(), colorEvent.getBorderStart(), colorEvent.getBorderEnd());
            });
            ((AccessGuiGraphics) (guiGraphics)).pose().translate(0.0F, 0.0F, 400.0F);
            int k1 = i1;

            int k2;
            ClientTooltipComponent clienttooltipcomponent2;
            for (k2 = 0; k2 < p_282615_.size(); ++k2) {
                clienttooltipcomponent2 = p_282615_.get(k2);
                clienttooltipcomponent2.renderText(preEvent.getFont(), l, k1, ((AccessGuiGraphics) (guiGraphics)).pose().last().pose(), guiGraphics.bufferSource());
                k1 += clienttooltipcomponent2.getHeight() + (k2 == 0 ? 2 : 0);
            }

            k1 = i1;

            for (k2 = 0; k2 < p_282615_.size(); ++k2) {
                clienttooltipcomponent2 = p_282615_.get(k2);
                clienttooltipcomponent2.renderImage(preEvent.getFont(), l, k1, guiGraphics);
                k1 += clienttooltipcomponent2.getHeight() + (k2 == 0 ? 2 : 0);
            }

            ((AccessGuiGraphics) (guiGraphics)).pose().popPose();
        }

    }
}
